
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:xim/common/get_style_view.dart';
import 'video_view_h5_controller.dart';
import 'video_view_h5_style.dart';

/// H5内嵌视频播放页面
class VideoViewH5Page extends GetStyleControllerView<dynamic, VideoViewH5Controller, VideoViewH5Style> {
  VideoViewH5Page({super.key});

  @override
  Widget buildView(BuildContext context, data) {
    return Scaffold(
      appBar: style.appBar,
      body: InAppWebView(
        key: controller.webViewKey,
        initialSettings: controller.settings,
        // initialUrlRequest: URLRequest(url: WebUri("http://localhost:8080/h5/video/index.html")),
        initialData: InAppWebViewInitialData(
          data: controller.pageData,
        ),
        onWebViewCreated: (ctl){
          // var loadString = rootBundle.loadString("assets/h5/video/index.html");
          controller.webViewController = ctl;
          // 获取m3u8清单文件下载地址
          ctl.addJavaScriptHandler(handlerName: 'getM3u8DownloadPath', callback: (arg){
            return controller.fileDownLoadUri;
          });
          // 明暗模式
          ctl.addJavaScriptHandler(handlerName: 'brightness', callback: (arg){
            return MediaQuery.of(context).platformBrightness == Brightness.light?'light':'dark';
          });
          // 解密m3u8文件中的key
          ctl.addJavaScriptHandler(handlerName: 'decodeKey', callback: (arg){
            return controller.decodeKey(arg[0]);
          });
        },
        onLoadStop: (ctl, utl){},
      ),
    );
  }
}